6. Inheritance and Collection

Lab Exercise 6-2: Use a Heterogeneous Collection of Customer Accounts

Objective

In this exercise you will create an heterogeneous array to represent the aggregation of customers to accounts. That is, a given customer may have several accounts of different types.

Directions

Start by changing your working directory to lab06/exercise2 on your computer.

Modify the Customer Class

  1. Create the banking directory. Copy the previous Banking project files in this package directory.
  2. Modify the Customer class to handle the accounts association with generic multiplicity, just as you did for in Lab Exercise 3-4. It must include the public methods: addAccount(Account), getAccount(int), and getNumOfAccounts().

Complete the TestBanking Program

This program creates a set of customers and accounts and generates a report of customers and their account balances.

In the TestBanking.java file you will find comment blocks that start and end with /*** ... ***/. These comments indicate the location in the code that you must supply.

  1. Use the instanceof operator to test what type of account we have and set account_type to an appropriate value, such as "Savings Account" or "Checking Account".
  2. Print out the type of account and the balance. Feel free to use the currency_format formatter to generate a "currency string" for the balance.
  3. Compile and run this program. You should see the following output.
                            CUSTOMERS REPORT
                            ================
     
    Customer: Simms, Jane
        Savings Account: current balance is $500.00
        Checking Account: current balance is $200.00
     
    Customer: Bryant, Owen
        Checking Account: current balance is $200.00
     
    Customer: Soley, Tim
        Savings Account: current balance is $1,500.00
        Checking Account: current balance is $200.00
     
    Customer: Soley, Maria
        Checking Account: current balance is $200.00
        Savings Account: current balance is $150.00